/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.problemas;

import com.mapping.Ticket;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
/**
 *
 * @author Andres
 */
@Namespace(value = "/")
@Action(value = "gestionProblemas", results = {@Result(name = "success", location = "/GestionProblemas/gestionProblemas.jsp")})
public class GestionProblemasAction extends ActionSupport{
    private List<Ticket> listaTickets;
    private String buscar;
    @Override
    public String execute(){
        String consulta1 = "select IDTICKET,DESCRIPCION,EMPATENDIO,EMPSOLICITO,PRIORIDAD,ESTADO,EMPRESOLVIO,FECHAINICIO,FECHAFIN,"
                            + "TIPOPROBLEMA,IDDISPOSITIVO,IDSOLUCION from ticket "
                            + "WHERE estado!=?1 and tipoProblema!=?2";
        String consulta2 = "select IDTICKET,DESCRIPCION,EMPATENDIO,EMPSOLICITO,PRIORIDAD,ESTADO,EMPRESOLVIO,FECHAINICIO,FECHAFIN,"
                            + "TIPOPROBLEMA,IDDISPOSITIVO,IDSOLUCION from ticket "
                            + "where match (idTicket,descripcion,empAtendio,EmpSolicito,prioridad,estado,EmpResolvio,tipoProblema,idDispositivo) "
                            + "against (?1 IN BOOLEAN MODE) AND estado!=?2 and tipoProblema!=?3";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        if(buscar== null || buscar.equals("")){
            Query query =  em.createNativeQuery(consulta1,Ticket.class);
            query.setParameter(1,"cerrado");
            query.setParameter(2,"RFC");
            listaTickets = (List<Ticket>) query.getResultList();
        }else{
            Query query =  em.createNativeQuery(consulta2,Ticket.class);
            query.setParameter(1,buscar);
            query.setParameter(2,"cerrado");
            query.setParameter(3,"RFC");
            listaTickets = (List<Ticket>) query.getResultList();
        }
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public List<Ticket> getListaTickets() {
        return listaTickets;
    }

    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }
    
}
